﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class ComicController
    {
        public IList<Comic> GetAllComic()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Comic>().ToList();
            }
        }

        public IList<String> GetAllComicTitle()
        {
            return GetAllComic().Select(c => c.Title).ToList();
        }

        public IList<ComicData> GetAllComicData()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var comics = session.Query<Comic>().ToList();
                return comics.Select(comic => new ComicData(comic.Id, comic.Title, 
                    comic.Author.Name, comic.Publisher.Name, comic.Year.Number.ToString(), 
                    comic.Rent.Rate.ToString(), comic.ComicStatus.Status, comic.Image)).ToList();
            }
        }

        public IList<Comic> GetAllComicAvailable()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Comic>().ToList()
                    .Where(c => c.ComicStatus.Status == "Available")
                    .Select(c => c)
                    .ToList();
            }
        }

        public IList<String> GetAllComicTitleAvailable()
        {
            return GetAllComicAvailable().Select(c => c.Title).ToList();
        }

        public IList<ComicData> GetAllComicDataAvailable()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var comics = session.Query<Comic>().ToList()
                    .Where(c => c.ComicStatus.Status == "Available")
                    .Select(c => c)
                    .ToList();

                return comics.Select(comic => new ComicData(comic.Id, comic.Title,
                    comic.Author.Name, comic.Publisher.Name, comic.Year.Number.ToString(),
                    comic.Rent.Rate.ToString(), comic.ComicStatus.Status, comic.Image)).ToList();
            }
        }

        public Comic GetComicById(Guid id)
        {
            return GetAllComic().Where(c => c.Id == id).Select(c => c).Single();
        }

        public Comic GetComic(Comic comic)
        {
            return GetAllComic().Where(c => c.Id == comic.Id).Select(c => c).Single();
        }

        public bool HasComicUrl(string url)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var comics = session.Query<Comic>().ToList();
                return comics.Any(comic => comic.Image == url);
            }
        }

        public string GetComicUrl(Comic comic)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Comic>().ToList().
                    Where(c => c.Id == comic.Id).
                    Select(c => c.Image).
                    Single();
            }
        }

        public void AddComic(Comic comic)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(comic);
                    transaction.Commit();
                }
            }
        }

        public void EditComic(Comic comic)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(comic);
                    transaction.Commit();
                }
            }
        }
        
        public void DeleteComic(Comic comic)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(comic);
                    transaction.Commit();
                }
            }
        }
    }

    public class ComicData
    {
        public ComicData(Guid id, string title, string author, string publisher, string year, string rent, string status, string image)
        {
            Id = id;
            Title = title;
            Author = author;
            Publisher = publisher;
            Year = year;
            Rent = rent;
            ComicStatus = status;
            Image = image;
        }

        public Guid Id { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public string Publisher { get; set; }
        public string Year { get; set; }
        public string Rent { get; set; }
        public string ComicStatus { get; set; }
        public string Image { get; set; }
    }
}
